package vlms.servlet.customer;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Account;
import vlms.bean.Person;
import vlms.bean.PremiumCustomer;
import vlms.bean.SimpleCustomer;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class SignUpServlet
 */
@WebServlet("/SignUp")
public class SignUp extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignUp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		
		HttpSession session = request.getSession(true); 
		int personid=0;
		
		 String fname = request.getParameter("firstname");
		 String lname=request.getParameter("lastname");
		 String username=request.getParameter("username");
		 String password=request.getParameter("password");
		 String email=request.getParameter("email");
		 String address=request.getParameter("address");
		 String city=request.getParameter("city");
		 String state=null;
		 String stateValues[]=request.getParameterValues("state"); 
		 
		 String zipcode=request.getParameter("zipcode");
		 String memberType=null;
		 String memberTypeValues[]=request.getParameterValues("Membertype"); 
		 String creditnumber=request.getParameter("creditcardno");
		 String cardtype=null;
		 String cardTypeValues[]=request.getParameterValues("creditcard_type"); 
		 String cvvnumber=request.getParameter("Cvvnumber");
		 
		 if(memberTypeValues.length==1)	 
		 {
			 
			 if(memberTypeValues[0].equals("SimpleCustomer"))
			 {
				 memberType="SC"; 
			 }
			 else
			 {
				 memberType="PC";
			 }
			 
			 System.out.println(memberType);
		 }
		 
		 
		 if(cardTypeValues.length==1)
			 
		 {
			 
			 if(cardTypeValues[0].equals("Visa"))
			 {
				cardtype="Visa"; 
			 }
			 else if(cardTypeValues[0].equals("Mastercard"))
			 {
				 cardtype="Mastercard";
			 }
			 else
			 {
				 cardtype="American Express";
			 }
			 
			 System.out.println(cardtype);
		 }
		 
		 if(stateValues!=null)
		 {
			 state=stateValues[0];
			 
		 }
		 System.out.println(fname + lname + username + password + email + address + city + state + zipcode + memberType +creditnumber + cardtype + cvvnumber);
		 Account account=new Account();
		 account.setUsername(username);
		 account.setPassword(password);
		 
		 Person person=new Person();
		 person.setFirstname(fname);
		 person.setLastname(lname);
		 person.setEmailid(email);
		 person.setAddress(address);
		 person.setCity(city);
		 person.setState(state);
		 person.setMembertype(memberType);
		 person.setZipcode(zipcode);
		 person.setCreditcardtype(cardtype);
		 person.setCreditcardnum(creditnumber);
		 person.setCreditcardcvv(Integer.parseInt(cvvnumber));
		 person.setCreditCardName(fname + lname);
		 person.setExpiryDate("09/2015");
		 
		 session.setAttribute("Person",person);
		 session.setAttribute("Account", account);
		 try
		 {
			 String result="";
			 if(memberType.equals("SC"))
			 {
				 System.out.println("Calling simple");
				 result=proxy.createSimpleCustomer(account, person);
			 }
			 else
			 {
				 result=proxy.createPremiumCustomer(account, person);
			 }
			 
			 if(result.equals("Account created successfully"))
			 {
				 Person tempPerson=proxy.retrievePersonByEmail(email);
				 personid=tempPerson.getPersonid();
				 session.setAttribute("Authenticated", "true");
				 session.setAttribute("MemberType", memberType);
				 if(memberType.equals("SC"))
				 {
				 SimpleCustomer simpleCustomer=proxy.retrieveSimpleCustomerByPersonId(String.valueOf(personid));
				 session.setAttribute("SimpleCustomer", simpleCustomer);
				 }
				 else
				 {
				 PremiumCustomer premiumCustomer=proxy.retrievePremiumCustomerByPersonId(String.valueOf(personid));
				 session.setAttribute("PremiumCustomer", premiumCustomer);
				 }
				 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/customer/home.jsp");
				 dispatcher.forward(request, response);
				 
				 //response.sendRedirect(request.getContextPath + "");
			 }
		 }
		 catch(RemoteException re)
		 {
			 re.getMessage();
			 re.printStackTrace();
		 }
		
	}

}
